package com.gxdf.extendedcontrols.gxdfclient.components;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;

import com.gxdf.extendedcontrols.gxdfclient.data.Field;
import com.gxdf.extendedcontrols.gxdfclient.data.ValidationMessage;
import com.gxdf.extendedcontrols.gxdfclient.data.ValidationResult;
import com.gxdf.extendedcontrols.gxdfclient.data.ValidationType;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;


public class GxdfCheckBox extends GxdfField implements OnClickListener{
	
	Context context;
	TextView lbl;
	HashMap<String,String> tempvalues;
	List<CheckBox> checks;
	

	public GxdfCheckBox(Context context,Field f) {
		super(context, f);
		
		this.context = context;
		this.setOrientation(LinearLayout.VERTICAL);
		//c.setChecked(selected);
		tempvalues=new HashMap<String, String>();
		checks = new ArrayList<CheckBox>();
		lbl = new TextView(context);
		lbl.setText(getFieldName());		
		
	}

	

	@Override
	public void drawField() {
		// TODO Auto-generated method stub
		this.addView(this.lbl);
	
		for(String s:getField().getType().getPosValues()){			
			
			CheckBox c= new CheckBox(this.context);
			
			if( getField().getValues().size() > 0 ){
				if(getField().getValues().get(0).equals(s)){
					c.setChecked(true);
				}
			}
			
			if(tempvalues.containsKey(s)) c.setChecked(true);			
			
			c.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
			c.setText(s);
			
			c.setOnClickListener(this);
			this.addView(c);
			checks.add(c);
			
		}
		
	
	}

	@Override
	public String getValue() {		
		return "";	
	}

	@Override
	public void saveValues() {		
		
		tempvalues.clear();
		for(CheckBox c: checks){
			
			if(c.isChecked()){				
				//actualValues.add(c.getText().toString());
				tempvalues.put(c.getText().toString(), c.getText().toString());
			}
		}
		
	}



	@Override
	public boolean isModified() {
		return tempvalues.equals(getField().getType().getPosValues());
	}



	@Override
	public List<String> getActualValues() {
		return (ArrayList<String>)this.tempvalues.values();
	}



	@Override
	public boolean errorFormat() {
		boolean invalid=true;
		
		if(getField().getFormatValidation().isEmpty()){
			invalid=false;
		}
		else{		
			//TODO: Ejecutar regexp. Si falla return true else return false	
			
			pattern = Pattern.compile(field.getFormatValidation());
			matcher = pattern.matcher(actualValues.get(0));
			invalid = matcher.matches();
		}
		
		return invalid;
	}



	@Override
	public boolean errorLength() {
		// TODO Auto-generated method stub
		return false;
	}



	@Override
	public boolean isFieldvalid() {		
		return isValid;
	}



	@Override
	public void onClick(View arg0) {
				
	}



	

}
